package framework.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import framework.jackson.Person;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Jackson 模块注册
 *
 * @author ME
 * @date 2021/1/17
 */
public class TestJacksonModules {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        OptionalInt optionalInt = OptionalInt.of(1);
        Optional<String> optional = Optional.of("YourBatman");
        IntStream intStream = IntStream.of(1, 2, 3);
        Stream<String> stream = Stream.of("1", "2", "3");
        System.out.println("\n注册 Jdk8Module 前：");
        testJdk8Module(mapper, optionalInt, optional, intStream, stream);
        mapper = new ObjectMapper();
        mapper.registerModule(new Jdk8Module());
        System.out.println("注册 Jdk8Module 后：");
        testJdk8Module(mapper, optionalInt, optional, intStream, stream);

        mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        Instant instant = Instant.now();
        System.out.println("\n注册 JavaTimeModule 前：");
        testJavaTimeModule(mapper, localDateTime, localDate, localTime, instant);
        System.out.println("注册 JavaTimeModule 后：");
        mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JavaTimeModule());
        testJavaTimeModule(mapper, localDateTime, localDate, localTime, instant);

        String jsonStr = "{\"name\":\"YourBatman\",\"age\":18}";
        System.out.println("\n注册 ParameterNamesModule 前：");
        System.out.println(mapper.readValue(jsonStr, Person.class));
        System.out.println("注册 ParameterNamesModule 后：");
        mapper = new ObjectMapper();
        mapper.registerModule(new ParameterNamesModule());
        System.out.println(mapper.readValue(jsonStr, Person.class));
    }

    public static void testJdk8Module(ObjectMapper mapper, OptionalInt optionalInt, Optional<String> optional, IntStream intStream, Stream<String> stream) throws JsonProcessingException {
        System.out.println(mapper.writeValueAsString(optionalInt));
        System.out.println(mapper.writeValueAsString(optional));
        System.out.println(mapper.writeValueAsString(intStream));
        System.out.println(mapper.writeValueAsString(stream));
    }

    private static void testJavaTimeModule(ObjectMapper mapper, LocalDateTime localDateTime, LocalDate localDate, LocalTime localTime, Instant instant) throws JsonProcessingException {
        System.out.println(mapper.writeValueAsString(localDateTime));
        System.out.println(mapper.writeValueAsString(localDate));
        System.out.println(mapper.writeValueAsString(localTime));
        System.out.println(mapper.writeValueAsString(instant));
    }
}
